import 'package:date_format/date_format.dart';
import 'package:flutter/cupertino.dart';

///@description
///
///@author: Murphy
///@create: 2021-07-16 19:18
class XdDateUtils {
  ///获取当前系统时间戳
  static int getCurrTimeStamp() {
    return DateTime.now().millisecondsSinceEpoch;
  }

  //距离当前时间是否小于2分钟
  static bool distanceCurrentTime(int? timestamp) {
    if (timestamp == null || timestamp == 0) {
      return false;
    }
    DateTime date = DateTime.fromMicrosecondsSinceEpoch(timestamp * 1000000);
    DateTime nowDate = DateTime.now();

    int minutes = nowDate.difference(date).inMinutes;
    if (minutes >= 0 && minutes < 2) {
      return true;
    }
    return false;
  }

  //用于IM聊天会话列表显示时间
  static String readTimestamp(int? timestamp) {
    if (timestamp == null || timestamp == 0) {
      return '';
    }
    DateTime date = DateTime.fromMicrosecondsSinceEpoch(timestamp * 1000000);
    DateTime nowDate = DateTime.now();

    if (nowDate.year > date.year ||
        nowDate.month > date.month ||
        nowDate.day - date.day >= 2) {
      //今年以前的显示年月日
      return formatDate(
        date,
        [yyyy, '/', m, '/', d],
      );
    } else if (nowDate.day - date.day == 1) {
      return '昨天';
    } else {
      return formatDate(date, [HH, ':', nn]);
    }
  }

  //用于IM聊天窗口显示时间 timestamp1小于timestamp2  如果 timestamp2 = -1 第一条历史消息
  static String chatReadTimestamp(int timestamp1, int timestamp2) {
    if (timestamp1 == 0 || timestamp2 == 0) {
      return '';
    }
    DateTime nowDate = DateTime.now();
    if (timestamp2 == -1) {
      //和当前时间作比较
      DateTime date1 =
          DateTime.fromMicrosecondsSinceEpoch(timestamp1 * 1000000);
      if (nowDate.year > date1.year ||
          nowDate.month > date1.month ||
          (nowDate.day - date1.day).abs() >= 2) {
        //两个时间相差2天以上
        return formatDate(
          date1,
          [yyyy, '-', m, '-', d, ' ', HH, ':', nn],
        );
      } else if (nowDate.day - date1.day == 1) {
        //两个时间相差1天
        return formatDate(
          date1,
          ['昨天', HH, ':', nn],
        );
      } else {
        return formatDate(date1, [HH, ':', nn]);
      }
    } else {
      DateTime date1 =
          DateTime.fromMicrosecondsSinceEpoch(timestamp1 * 1000000);
      DateTime date2 =
          DateTime.fromMicrosecondsSinceEpoch(timestamp2 * 1000000);
      if (date2.difference(date1).inMinutes.abs() >= 5) {
        if (date2.year < nowDate.year ||
            date2.month < nowDate.month ||
            (nowDate.day - date2.day).abs() >= 2) {
          return formatDate(
            date2,
            [yyyy, '-', m, '-', d, ' ', HH, ':', nn],
          );
        } else if (nowDate.day - date2.day == 1) {
          return formatDate(
            date2,
            ['昨天', HH, ':', nn],
          );
        } else {
          return formatDate(date2, [HH, ':', nn]);
        }
      }
    }
    return '';
  }

  //用于背包列表过期时间
  static String overdueTimestamp(int timestamp, {bool isDetailed = false}) {

    DateTime date = DateTime.fromMicrosecondsSinceEpoch(timestamp * 1000);
    DateTime nowDate = DateTime.now();
    if (isDetailed) {
      String str = '';
      int day = date.difference(nowDate).inDays;
      int hours = date.difference(nowDate).inHours - day * 24;
      int minutes =
          date.difference(nowDate).inMinutes - day * 24 * 60 - hours * 60;
      if (day > 0) {
        str = '$day天';
      }
      if (hours > 0) {
        str = '$str$hours小时';
      }
      if (minutes > 0) {
        str = '$str$minutes分钟';
      }
      if (str == '') {
        str = '1分钟';
      }
      return str;
    }

    if (date.difference(nowDate).inDays > 0) {
      return '${date.difference(nowDate).inDays}天';
    } else {
      if (date.difference(nowDate).inHours > 0) {
        return '${date.difference(nowDate).inHours}小时';
      }
      return '${date.difference(nowDate).inMinutes <= 0 ? 0 : date.difference(nowDate).inMinutes}分钟';
    }
  }


  ///时间戳 转 月日
  static String timestampToMD(int timestamp) {
    if (timestamp == null || timestamp == 0) {
      return '';
    }
    DateTime date = DateTime.fromMicrosecondsSinceEpoch(timestamp * 1000);

    return formatDate(
      date,
      [mm, '月', dd, '日'],
    );
  }

  //用于战绩列表显示时间
  static String recordTimestamp(int? timestamp) {
    if (timestamp == null || timestamp == 0) {
      return '';
    }
    DateTime date = DateTime.fromMicrosecondsSinceEpoch(timestamp * 1000);
    DateTime nowDate = DateTime.now();

    if (nowDate.year > date.year || nowDate.month > date.month) {
      //今年以前的显示年月日
      return formatDate(
        date,
        [yyyy, '-', m, '-', d],
      );
    } else if (nowDate.day == date.day) {
      return formatDate(date, ['今天 ', HH, ':', nn]);
    } else {
      return formatDate(date, [m, '-', d, ' ', HH, ':', nn]);
    }
  }

  ///时间戳 转 2022-01-04 09:00:00
  static String timestampToYMD(int timestamp) {
    if (timestamp == null || timestamp == 0) {
      return '';
    }
    DateTime date = DateTime.fromMicrosecondsSinceEpoch(timestamp * 1000);

    return formatDate(date, [yyyy, '-', m, '-', d, ' ', HH, ':', nn, ':', ss],);
  }


  ///时间戳 转 2022-01-04 09:00
  static String timestampToYMDHN(int? timestamp) {
    if (timestamp == null || timestamp == 0) {
      return '';
    }
    DateTime date = DateTime.fromMicrosecondsSinceEpoch(timestamp * 1000);

    return formatDate(date, [yyyy, '-', m, '-', d, ' ', HH, ':', nn],);
  }

  ///时间戳 转 2022-01-04
  static String timeToYMD(int timestamp) {
    if (timestamp == null || timestamp == 0) {
      return '';
    }
    DateTime date = DateTime.fromMicrosecondsSinceEpoch(timestamp * 1000);

    return formatDate(date, [yyyy, '.', m, '.', d],);
  }

  ///时间戳 转 01-04 12:05
  static String timeToMDHN(int timestamp) {
    if (timestamp == null || timestamp == 0) {
      return '';
    }
    DateTime date = DateTime.fromMicrosecondsSinceEpoch(timestamp * 1000);

    return formatDate(date, [ m, '-', d, ' ', HH, ':', nn],);
  }

}

class HexColor extends Color {
  static int _getColorFromHex(String hexColor) {
    hexColor = hexColor.toUpperCase().replaceAll("#", "");
    hexColor = hexColor.replaceAll('0X', '');
    if (hexColor.length == 6) {
      hexColor = "FF" + hexColor;
    }
    return int.parse(hexColor, radix: 16);
  }

  HexColor(final String hexColor) : super(_getColorFromHex(hexColor));
}
